import React from "react";
import { useLocation, useNavigate } from "react-router-dom";
import * as apis from "@/apis";

/** 设置当前页面的订单状态 */
export function useOrderStatus() {

    // 获取地址栏参数 ab
    const navigate = useNavigate();
    const location = useLocation();

    const orderStatus = React.useMemo<apis.order.OrderStatus>(() => {
        return new URLSearchParams(location.search).get('orderStatus') as any || apis.order.OrderStatus.待安排;
    }, [location.search]);

    const setOrderStatus = React.useCallback((status: apis.order.OrderStatus) => {
        const search = new URLSearchParams(window.location.search);
        search.set('orderStatus', `${status}`);
        navigate({
            pathname: location.pathname,
            search: search.toString(),
        }, {
            state: location.state
        });
    }, [location.pathname, location.state, navigate]);

    return {
        orderStatus,
        setOrderStatus
    }
}